<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="申报课题">
              <a-input v-model="queryParam.name" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item label="指导老师">
              <a-input v-model="queryParam.instructor" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 6) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="handlerApply" :disabled="!applyAble">项目申报</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

        <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'Project:add'">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('社团')" v-has="'Project:edit'">导出</a-button> -->
      
      <!-- <a-button type="primary" icon="import" @click="visibleImport=true" v-has="'Project:edit'">导入</a-button> -->
      <!-- <XImport v-model="visibleImport" moduleName="researchProject" @ok="searchQuery" @cancel="visibleImport=false"></XImport> -->

      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'Project:delete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" v-has="'Project:delete'"/>批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <s-table ref="table" size="default" rowKey="id" :scroll="{x:true}"  :columns="columns" :data="getData" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"  @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a style="margin-left:10px" @click="handlePreview(record.id)">查看</a>
        <a-popconfirm v-if="record.cancelFlag ==1" style="margin-left:10px" title="确定撤销该记录?" @confirm="() => handleRevoke(record.id)">
          <a>撤销</a>
        </a-popconfirm>
        <a @click="handleEdit(record)" style="margin-left:10px">编辑</a>
        <a-popconfirm style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>

    <!-- 申请弹窗 -->
    <ProjectModal ref="modalForm" @ok="handleOk"></ProjectModal>

    <!-- 查看 -->
    <CheckModal :bizId="detail.id" bizComponent="/project/modules/ProjectBuild" :printModel="true" v-model="detail.visible" ref="CheckModal"></CheckModal>
  
  </a-card>
</template>

<script>
import ProjectModal from './modules/ProjectModal'
import { getProjectsMyList,delProject } from '@/api/api'
import { httpAction, getAction,deleteAction } from '@/api/manage'
import STable from '@/components/table/'
import XImport from '@/components/extends/XImport.vue'
import find from 'lodash.find'
import moment from 'moment'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { DisabledAuthFilterMixin } from '@/mixins/DisabledAuthFilterMixin'

import CheckModal from '@/components/extends/CheckModel'
export default {
  components:{
    CheckModal,
    STable,
    XImport,
    ProjectModal
  },
  mixins:[JeecgListMixin,DisabledAuthFilterMixin],
  data(){
    return {
      detail:{},
      stateMap:{
        0:'草稿',
        1:'审批中',
        10:'已过审'
      },
      url: {
        list:'/researchProject/list',
        config: '/bizConfig/list',
        deleteBatch:'/researchProject/deleteBatch',
        revoke:'/workflow/cancelProcess'
      },
      disableMixinCreated:true,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam:{
      },
      configList:[],
      visibleImport:false,
      columns:[
        {
          title: '项目申报课题',
          align: 'center',
          width: 250,
          dataIndex: 'name'
        },
        {
          title: '参与人员',
          align: 'center',
          width: 150,
          dataIndex: 'members'
        },
        {
          title: '指导老师',
          align: 'center',
          width: 150,
          dataIndex: 'instructor'
        },
        {
          title: '负责人',
          align: 'center',
          width: 150,
          dataIndex: 'director'
        },
        {
          title: '起止年月',
          align: 'center',
          dataIndex: 'startTime',
          width: 150,
          customRender(text,record){
            return (record.startTime?moment(record.startTime).format('YYYY-MM-DD'):'-') + ' 至 '+ (record.endTime?moment(record.endTime).format('YYYY-MM-DD'):'-')
          }
        },
        {
          title: '申请经费',
          align: 'center',
          width: 150,
          dataIndex: 'fundAmount'
        },
        {
          title: '状态',
          align: 'center',
          width: 150,
          dataIndex: 'actStatus',
          customRender:(text,record)=>{
            return this.stateMap[text]
          }
        },
        {
          title: '当前审核人',
          align:"center",
          width: 150,
          dataIndex: 'waitHandler'
        },
        {
          title: '操作',
          align: 'center',
          width: 220,
          fixed:"right",
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys:[],
      selectedRows:[],
      getData:parameter => {
        return getProjectsMyList(Object.assign({},parameter, this.queryParam)).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      }
    }
  },
  computed:{
    applyAble(){
      let conf = find(this.configList,{'type':'TIME_RANGE'})
      if(conf){
        let arr = conf.value.split('-')
        let now = +new Date()
        let start = +new Date(parseInt(arr[0]))
        let end = +new Date(parseInt(arr[1]))
        if(start<now&&now<end){
          return true
        }
      }
      return false
    }
  },
  mounted(){
    this.loadConfig()
  },
  methods:{
    loadConfig() {
      getAction(this.url.config, {
        module: 'research_project'
      }).then(res => {
        if (res.success && res.result) {
          this.configList = res.result.records
        } else {
          this.configList = []
        }
      })
    },
    handleAdd(){
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.add();
    },
    handleOk(){
      this.$refs.table.refresh()
    },
    handleRevoke(id){
      postAction(this.url.revoke, {
        bizId: id
      }).then(res => {
        if(res.success){
          this.$message.success('撤销成功');
          this.searchQuery()
        }else{
          this.$message.error(res.message)
        }
      })
    },
    handlerApply(){
      this.$refs.modalForm.apply({
        director:{
          directorId: this.$store.getters.userInfo.id,
          name: this.$store.getters.userInfo.username,
        }
      })
    },
    handleEdit(record){
      this.$refs.modalForm.title = '编辑项目'
      this.$refs.modalForm.actStatus = record.actStatus
      this.$refs.modalForm.edit(record)
    },
    handleDelete(id){
      delProject({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    handlePreview(id){
      // this.$refs.modalForm.title = '查看'
      // this.$refs.modalForm.footer = false
      // this.$refs.modalForm.preview({id:id})
      this.detail['id'] = id
      this.$set(this.detail,'visible',true)
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    searchReset() {
      this.queryParam = {
        
      }
      this.searchQuery();
    },
    searchQuery(){
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    onChange(){

    }
  }
}
</script>

<style></style>
